package com.ruoyi.tongyan.controller;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.mysql.cj.protocol.x.Notice;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.service.ISysNoticeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.tongyan.domain.SysUserNotice;
import com.ruoyi.tongyan.service.ISysUserNoticeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户‑通知映射Controller
 * 
 * @author ruoyi
 * @date 2025-05-07
 */
@RestController
@RequestMapping("/tongyan/notice")
public class SysUserNoticeController extends BaseController
{
    @Autowired
    private ISysUserNoticeService sysUserNoticeService;
    @Autowired
    private ISysNoticeService noticeService;
    /**
     * 查询用户‑通知映射，返回完整 Notice 对象列表
     */
    @PreAuthorize("@ss.hasPermi('system:notice:list')")
    @GetMapping("/list")
    public AjaxResult list(SysUserNotice sysUserNotice)
    {
        // 1. 拿到用户‑通知映射
        List<SysUserNotice> mappings = sysUserNoticeService.selectSysUserNoticeList(sysUserNotice);
        // 2. 提取所有 noticeId
        List<Long> noticeIds = mappings.stream()
                .map(SysUserNotice::getNoticeId)
                .collect(Collectors.toList());
        if (noticeIds.isEmpty()) {
            return AjaxResult.success(Collections.emptyList());
        }
        // 3. 批量查询公告详情（需要在 NoticeService 中实现 selectNoticeByIds）
        List<SysNotice> notices = noticeService.selectNoticeByIds(noticeIds);
        // 4. 返回完整列表
        return AjaxResult.success(notices);
    }


    /**
     * 导出用户‑通知映射列表
     */
    @PreAuthorize("@ss.hasPermi('system:notice:export')")
    @Log(title = "用户‑通知映射", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUserNotice sysUserNotice)
    {
        List<SysUserNotice> list = sysUserNoticeService.selectSysUserNoticeList(sysUserNotice);
        ExcelUtil<SysUserNotice> util = new ExcelUtil<SysUserNotice>(SysUserNotice.class);
        util.exportExcel(response, list, "用户‑通知映射数据");
    }

    /**
     * 获取用户‑通知映射详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:notice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysUserNoticeService.selectSysUserNoticeById(id));
    }

    /**
     * 新增用户‑通知映射
     */
    @PreAuthorize("@ss.hasPermi('system:notice:add')")
    @Log(title = "用户‑通知映射", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysUserNotice sysUserNotice)
    {
        return toAjax(sysUserNoticeService.insertSysUserNotice(sysUserNotice));
    }

    /**
     * 修改用户‑通知映射
     */
    @PreAuthorize("@ss.hasPermi('system:notice:edit')")
    @Log(title = "用户‑通知映射", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysUserNotice sysUserNotice)
    {
        return toAjax(sysUserNoticeService.updateSysUserNotice(sysUserNotice));
    }

    /**
     * 删除用户‑通知映射
     */
    @PreAuthorize("@ss.hasPermi('system:notice:remove')")
    @Log(title = "用户‑通知映射", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysUserNoticeService.deleteSysUserNoticeByIds(ids));
    }
}
